package com.xtck.ha.fault.core.controller;

import com.xtck.ha.common.core.constant.HttpStatus;
import com.xtck.ha.common.core.utils.poi.ExcelUtil;
import com.xtck.ha.common.core.web.controller.BaseController;
import com.xtck.ha.common.core.web.domain.AjaxResult;
import com.xtck.ha.common.core.web.page.TableDataInfo;
import com.xtck.ha.fault.core.model.DsModInfo;
import com.xtck.ha.fault.core.service.DsModInfoService;
import com.xtck.ha.fault.framework.utils.JsonResult;
import com.xtck.ha.fault.modules.pojo.entity.ModInfo;
import com.xtck.ha.fault.modules.pojo.vo.ModInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 模型列表 controller
 *
 * @ClassName: DsModInfoController
 * @Description: 外部访问接口
 * @author zhoutanxin
 * @date 2025-07-01
 */
@Api(tags = "模型信息管理")
@RestController
@RequestMapping("/modinfo")
public class DsModInfoController extends BaseController {

    @Autowired
    private DsModInfoService dsModInfoService;


    @ApiOperation("分页查询模型信息列表")
    @GetMapping("/page")
    public AjaxResult page(DsModInfo dsModInfo)
    {
        startPage();
        List<DsModInfo> list = dsModInfoService.selectTList(dsModInfo);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setList(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(dsModInfoService.count(dsModInfo));
        return AjaxResult.success(rspData);
    }

    @ApiOperation("列表查询模型信息")
    @GetMapping("/list")
    public List<DsModInfo> list(DsModInfo dsModInfo)
    {
        List<DsModInfo> list = dsModInfoService.selectTList(dsModInfo);
        return list;
    }

    @GetMapping("/all")
    @ApiOperation(value = "获取所有模型")
    public AjaxResult getAll(){
        List<DsModInfo> list = dsModInfoService.selectTList(DsModInfo.builder().build());
        if (null != list && list.size() != 0)
            return AjaxResult.success(list);
        else
            return AjaxResult.error("未查询到模型信息");
    }

    @ApiOperation("导出模型信息列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DsModInfo dsModInfo)
    {
        List<DsModInfo> list = dsModInfoService.selectTList(dsModInfo);
        ExcelUtil<DsModInfo> util = new ExcelUtil<>(DsModInfo.class);
        util.exportExcel(response, list, "样例数据");
    }

    @ApiOperation("获取$模型信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dsModInfoService.selectTById(id));
    }

//    @ApiOperation("新增$模型信息")
//    @PostMapping("/add")
//    public AjaxResult add(@RequestBody DsModInfo dsModInfo)
//    {
//        return toAjax(dsModInfoService.insertT(dsModInfo));
//    }

    @PostMapping("add")
    @ApiOperation(value = "新增$模型信息")
    public AjaxResult save(DsModInfo vo, MultipartFile modFile){
        return dsModInfoService.add(vo,modFile);
    }

    @ApiOperation("修改模型信息")
    @PutMapping
    public AjaxResult update(@RequestBody DsModInfo dsModInfo)
    {
        dsModInfo.setUpdateTime(new Date());
        int i = dsModInfoService.updateT(dsModInfo);
        return toAjax(i);
    }

    @ApiOperation("删除模型信息")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dsModInfoService.deleteTByIds(ids));
    }

    /**
     * 启动模型服务
     * @param modInfo 模型modInfo
     * @return JsonResult
     */
    @PostMapping("startServer")
    @ApiOperation(value = "启动算法服务")
    public AjaxResult startAlgorithmServer(@RequestBody DsModInfo modInfo) {
        return dsModInfoService.startAlgorithmServer(modInfo);
    }

    /**
     * 停止模型服务
     * @param modInfo 模型modInfo
     * @return JsonResult
     */
    @PostMapping("stopServer")
    @ApiOperation(value = "停止算法服务")
    public AjaxResult stopAlgorithmServer(@RequestBody DsModInfo modInfo) {
        return dsModInfoService.stopAlgorithmServer(modInfo);
    }

}
